package com.bill99.codecheck.sonar.orm.manager.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.util.StringUtils;

import com.bill99.codecheck.sonar.orm.dao.JiraDataDao;
import com.bill99.codecheck.sonar.orm.entity.JiraItem;
import com.bill99.codecheck.sonar.orm.manager.JiraDataMng;
import com.bill99.riaframework.common.dto.JiraOnlineDemandDto;

public class JiraDataMngImpl implements JiraDataMng{

	private JiraDataDao jiraDataDao;
	private String roleString;
    
	
	public void setRoleString(String roleString) {
		this.roleString = roleString;
	}

	public void setJiraDataDao(JiraDataDao jiraDataDao) {
		this.jiraDataDao = jiraDataDao;
	}
	
	List<String> roleList = new ArrayList<String>();
	@Override
	public JiraItem findJiraInfo(String email) {
		List<JiraItem> jiraInfoList = jiraDataDao.findJiraInfo(email);
		JiraItem jiraItemResult = new JiraItem();
		Set<String> aSet = new HashSet<String>();
		Set<String> bSet = new HashSet<String>();
		Set<String> cSet = new HashSet<String>();
		
		List<String> aList = new ArrayList<String>();
		List<String> bList = new ArrayList<String>();
        String authorname="";

        Set<String>  dSet = new HashSet<String>();
		List<String> dList = new ArrayList<String>();

		for(JiraItem jiraItem : jiraInfoList){
			aSet.add(jiraItem.getProjectName());
//			bSet.add(jiraItem.getLeadEmail().concat("@99bill.com"));
			cSet.add(jiraItem.getProjectId());
			authorname = jiraItem.getAuthorName();
		}
		
		for(String projectname: aSet){
			aList.add(projectname);	
		}
		for(String leademail: bSet){
			bList.add(leademail);	
		}
		jiraItemResult.setProjectNameList(aList);
//		jiraItemResult.setLeadEmailList(bList);
		jiraItemResult.setAuthorName(authorname);
		List<String> projectIdSet = new ArrayList<String>();
		for(String aString : cSet){
			projectIdSet.add(aString);
		}
		
		if(StringUtils.hasLength(roleString)){
			roleList.addAll(Arrays.asList( roleString.split(",")));
		}
		//判断projectIdSet roleList 如果为空不获取email
		if(projectIdSet.size()>0 && roleList.size()>0){
		List<JiraItem> jiraInfoList22 = jiraDataDao.findJiraTLemail(projectIdSet,roleList);
		for(JiraItem jiraItem : jiraInfoList22){
			dSet.add(jiraItem.getEmail());
		}
		
		for(String emailitem: dSet){
			dList.add(emailitem);	
		}
		jiraItemResult.setEmailList(dList);
		}
		return jiraItemResult;
	}
	public List<JiraOnlineDemandDto> getJiraOnlineDemonByOnLineId(Set<String> onLineSet) {

		return jiraDataDao.getJiraOnlineDemonByOnLineId(onLineSet);
	}

	@Override
	public List<String> getwaibaoJiraemailList(String projectname) {
		List<String> projectIdSet = jiraDataDao.findJiraProjectId(projectname);
		List<JiraItem> jiraItemlist = null;
		
		if(projectIdSet.size()>0 && roleList.size()>0){
			jiraItemlist = jiraDataDao.findJiraTLemail(projectIdSet, roleList);
		}
		Set<String> emailset = new HashSet<String>();
		List<String> emaillist = new ArrayList<String>();

		for(JiraItem jiraItem : jiraItemlist){
			emailset.add(jiraItem.getEmail());
		}
		for(String emailitem: emailset){
			emaillist.add(emailitem);	
		}
		return emaillist;
	}

}
